﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VisionRoc.Tool
{
    public class HexHelper
    {

        public static byte[] strToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", " ");
            if ((hexString.Length % 2) != 0)
            {
                hexString += " ";
            }
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
            }
            return returnBytes;
        } 


        /// <summary>
        /// 字符串转16进制字符
        /// </summary>
        /// <param name="_str">字符串</param>
        /// <param name="encode">编码格式</param>
        /// <returns></returns>
        public static string StringToHexString(string _str, Encoding encode)
        {
            //去掉空格
            _str = _str.Replace(" ", "");
            //将字符串转换成字节数组。
            byte[] buffer = encode.GetBytes(_str);
            //定义一个string类型的变量，用于存储转换后的值。
            string result = string.Empty;
            for (int i = 0; i < buffer.Length; i++)
            {
                //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                result += Convert.ToString(buffer[i], 16);
            }
            return result;
        }


        /// <summary>
        /// 16进制字符转字符串
        /// </summary>
        /// <param name="hex">16进制字符</param>
        /// <param name="encode">编码格式</param>
        /// <returns></returns>
        public static string HexStringToString(string hex, Encoding encode)
        {
            byte[] buffer = new byte[hex.Length / 2];
            string result = string.Empty;
            for (int i = 0; i < hex.Length / 2; i++)
            {
                result = hex.Substring(i * 2, 2);
                buffer[i] = Convert.ToByte(result, 16);
            }
            //返回指定编码格式的字符串
            return encode.GetString(buffer);
        }


    }
}
